﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NLog;
using WisDom.IService;
using WisDom.Models;
using WisDom.Util;


// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WisDom.Update.Controllers
{
    /// <summary>
    /// 客户端文件信息
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ClientFileInfoController : ControllerBase
    {
        IBackupClientFileInterface _clientFileBack = null;

        public ClientFileInfoController(IBackupClientFileInterface clientFileBack)
        {
            _clientFileBack = clientFileBack;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="BackupType">备份类型</param>
        /// <param name="pageSize">页显示数量</param>
        /// <param name="pageIndex">显示的页面</param>
        /// <returns></returns>
        [HttpGet(Name = "GetClientFilePageData")]
        public async Task<IActionResult> Get([FromQuery]string BackupType, [FromQuery]int pageSize, [FromQuery]int pageIndex)
        { 
            try
            {
                var pagination = new Util.Pagination { PageIndex = pageIndex, Take = pageSize };
                var lst = await _clientFileBack.GetPageList(pagination, new { BackupType = BackupType }.ToJson());
                return new JsonResult(new { code = "0", msg = "获取数据成功", count = pagination.Records, data = lst });
            }
            catch (Exception ex)
            {
                return new JsonResult(new { code = "1", msg = ex.Message, count = 0 });
            }
        } 
        // GET api/<controller>/5
        [HttpGet("{id}",Name ="GetClientFileById")]
        public async Task<IActionResult> Get(int id)
        {
            var entity = await _clientFileBack.GetEntity(id.ToString());
            return new JsonResult(entity);
        }

        // POST 新增
        [HttpPost]
        public async Task<bool> Post([FromBody]BackupClientFileEntity entity)
        {
            return await _clientFileBack.InsertData(entity) > 0 ? true : false;
        }

        // PUT api/<controller>/5
        [HttpPut("{id}")]
        public async Task<bool> Put(int id, [FromBody]BackupClientFileEntity entity)
        {
            return await _clientFileBack.UpdateData(id.ToString(), entity) > 0 ? true : false;
        }

        // DELETE api/<controller>/5
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _clientFileBack.RemoveData(id.ToString()) > 0 ? true : false;
        }
    }


}
